package zdc.display {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.sampler.getSampleCount;

import zdc.Util;

/**
 * Fp10软驱动版Coverflow
 */
public class CoverFlowFp10 extends Sprite {
    protected var posFaces:Vector.<DisplayObject>;
    protected var refFaces:Vector.<DisplayObject>;
    protected var faceUrls:Array;
    protected var sx:Number = 0;
    protected var startIdx:int = 1;
    protected var startRotY:Number = 80;
    protected var sy:Number = 0;
    protected var itemHei:Number = 350;
    protected var itemWid:Number = 550;
    protected var margX:Number = 0;
    protected var zero:Point = new Point();
    protected var gradDio:DisplayObject = null;
    protected var gradBpd:BitmapData = null;

    public function CoverFlowFp10(faces:Array = null):void {
        this.faceUrls = faces;
        gradDio = new Grad();
        if (gradDio) {
            gradBpd = Util.sizeBpd(Util.getDioBpd(gradDio), itemWid, itemHei);
        }
        if (faceUrls) {

        }

        initPreVars();
        initPreStates();
        stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        initPostVars();
        initStates();
        initEvents();
        run();
    }

    private function initPreVars():void {

    }

    private function initPreStates():void {

    }

    private function initPostVars():void {

    }

    private function initStates():void {

    }

    private function initEvents():void {

    }

    private function run():void {

    }

    /**
     * 设置图片对象
     * @param arr
     */
    protected function set faces(arr:Vector.<DisplayObject>):void {
        removeResources();
        if (!arr) {
            return;
        }

        var i:int = 0;
        var bmp:Bitmap;
        //注意，matrix的tx和ty不是负的！！！！！！！！！！！！！
        var matrix:Matrix = new Matrix(1, 0, 0, 1, itemWid / 2, itemHei / 2);
        var dio:DisplayObject;
        posFaces = new Vector.<DisplayObject>();
        refFaces = new Vector.<DisplayObject>();

        for (i = 0; i < arr.length; i++) {
            dio = arr[i];
            posFaces.push(dio);
            addChild(dio);

            var sp:Sprite = new Sprite();
            sp.mouseChildren = false;
            sp.mouseEnabled = false;
            var bpd:BitmapData;
            bpd = new BitmapData(itemWid, itemHei, true, 0xff0);
            bpd.copyPixels(Util.getDioBpd(dio, false, matrix), bpd.rect, zero, gradBpd, zero, true);

            bmp = new Bitmap(bpd);
            sp.addChild(bmp);
            bmp.x = -(bmp.width >> 1);
            bmp.y = -(bmp.height >> 1);
            refFaces.push(sp);
            addChild(sp);
        }

        setFacesPos();
    }

    /**
     *设置封面位置
     */
    protected function setFacesPos():void {
        if (!posFaces || posFaces.length < 1) {
            return;
        }

        var i:int = 0;
        var pos:DisplayObject;
        var ref:DisplayObject;
        for (i = 0; i < posFaces.length; i++) {
            pos = posFaces[i];
            ref = refFaces[i];
            ref.x = pos.x = (stage.stageWidth >> 1) + (i - startIdx) * (itemWid + margX);
            pos.rotationY = ref.rotationY = (i == startIdx ? 0 : (i < startIdx ? -1 : 1)) * startRotY - (i - startIdx) * 5;
            ref.scaleY = -1;
            pos.y = stage.stageHeight >> 1;
            ref.y = pos.y + itemHei;
            if (i == startIdx) {
                Util.top(pos);
            }
        }
    }

    public function get urlOfFaces():Array {
        return faceUrls;
    }

    public function set urlOfFaces(arr:Array):void {

    }

    /**
     * 释放资源
     */
    protected function removeResources():void {
        removeFaces();
    }

    /**
     * 删除图片对象
     */
    protected function removeFaces():void {
        var dio:DisplayObject;
        for each(dio in posFaces) {
            removeChild(dio);
        }
        for each(dio in refFaces) {
            removeChild(dio);
        }
        posFaces = null;
        refFaces = null;
    }
}
}